﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HIWSystem.Common;
using HIWSystem.Models;

namespace HIWSystem.ViewModel
{
    public class UpdateStationViewModel :ViewModelBase
    {
        public UpdateStationViewModel()
        {
            Messenger.Default.Register<StationsEntity>(this, "UpdateStationInfo", ReceiveAndShowStationInfo);
        }
        #region 属性
        private ObservableCollection<StationTypeEntity> stationTypeResource;
        /// <summary>
        /// 站位类型实体类集
        /// </summary>
        public ObservableCollection<StationTypeEntity> StationTypeResource
        {
            get { return stationTypeResource; }
            set { stationTypeResource = value; this.RaisePropertyChanged(() => StationTypeResource); }
        }
        private StationTypeEntity selectedStationTypeEntity;
        /// <summary>
        /// 选择的站位类型实体<ComboBox控件选中的内容>
        /// </summary>
        public StationTypeEntity SelectedStationTypeEntity
        {
            get { return selectedStationTypeEntity; }
            set { selectedStationTypeEntity = value; this.RaisePropertyChanged(() => SelectedStationTypeEntity); }
        }
        private string station;
        /// <summary>
        /// 站位
        /// </summary>
        public string Station
        {
            get { return station; }
            set { station = value; this.RaisePropertyChanged(() => Station); }
        }
        private string stationDesc;
        /// <summary>
        /// 站位描述
        /// </summary>
        public string StationDesc
        {
            get { return stationDesc; }
            set { stationDesc = value; this.RaisePropertyChanged(() => StationDesc); }
        }

        private ObservableCollection<string> packingCheckFlagSource;
        /// <summary>
        /// 包装工序检查标记资源
        /// </summary>
        public ObservableCollection<string> PackingCheckFlagSource
        {
            get { return packingCheckFlagSource; }
            set { packingCheckFlagSource = value; this.RaisePropertyChanged(() => PackingCheckFlagSource); }
        }
        private string packingCheckFlag;
        /// <summary>
        /// 包装检查标记
        /// </summary>
        public string PackingCheckFlag
        {
            get { return packingCheckFlag; }
            set { packingCheckFlag = value; this.RaisePropertyChanged(() => PackingCheckFlag); }
        }
        private ObservableCollection<string> endCheckFlagSource;
        /// <summary>
        ///  入库检查标记资源
        /// </summary>
        public ObservableCollection<string> EndCheckFlagSource
        {
            get { return endCheckFlagSource; }
            set { endCheckFlagSource = value; this.RaisePropertyChanged(() => EndCheckFlagSource); }
        }

        private string endCheckFlag;
        /// <summary>
        /// 入库检查标记
        /// </summary>
        public string EndCheckFlag
        {
            get { return endCheckFlag; }
            set { endCheckFlag = value; this.RaisePropertyChanged(() => EndCheckFlag); }
        }
        #endregion

        #region 命令
        private RelayCommand updateCmd;

        public RelayCommand UpdateCmd
        {
            get {
                if (updateCmd==null)
                {
                    updateCmd = new RelayCommand(Update);
                }
                return updateCmd; }
            set { updateCmd = value; }
        }
        private RelayCommand loadedPageCmd;

        public RelayCommand LoadedPageCmd
        {
            get {
                if (loadedPageCmd==null)
                {
                    loadedPageCmd = new RelayCommand(LoadedPage);
                }
                return loadedPageCmd; }
            set { loadedPageCmd = value; }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 更新操作
        /// </summary>
        private void Update()
        {
            string retMsg = string.Empty;
            StationsEntity entity = new StationsEntity()
            {
                Station=this.Station??"",
                StationDesc=this.StationDesc??"",
                StationType = SelectedStationTypeEntity?.StationTypeEn ?? "",
                PackingCheckFlag = this.PackingCheckFlag ?? "",
                EndCheckFlag = this.EndCheckFlag ?? ""
            };
            retMsg = BusinessProcess.UpdateStationInfo(entity);
            if (retMsg.Split('|')[0] == "Y")
            {
                MessageBox.Show(retMsg.Split('|')[1].ToString(), "软件温馨提示!", MessageBoxButton.OK, MessageBoxImage.Information);
                Messenger.Default.Send("更新完成", "CompleteStationUpdate");
            }
            else
            {
                MessageBox.Show(retMsg.Split('|')[1].ToString(), "软件温馨提示!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 页面加载
        /// </summary>
        private void LoadedPage()
        {
            StationTypeResource = new ObservableCollection<StationTypeEntity>
            {
              new StationTypeEntity{Id=0,StationTypeCn="正常",StationTypeEn="Normal"},
              new StationTypeEntity{Id=1,StationTypeCn="质量",StationTypeEn="QC"},
              new StationTypeEntity{Id=2,StationTypeCn="测试",StationTypeEn="Test"}
            };
            PackingCheckFlagSource = new ObservableCollection<string> { "Y", "N" };
            EndCheckFlagSource = new ObservableCollection<string> { "Y", "N" };
        }

        private void ReceiveAndShowStationInfo(StationsEntity obj)
        {
            if (obj != null)
            {
                this.Station = obj.Station ?? "";
                this.StationDesc = obj.StationDesc ?? "";
                this.SelectedStationTypeEntity = StationTypeResource.Single(x => x.StationTypeEn == obj.StationType);
                this.PackingCheckFlag = PackingCheckFlagSource.Single(x => x == obj.PackingCheckFlag);
                this.EndCheckFlag = EndCheckFlagSource.Single(x => x == obj.EndCheckFlag);
            }
        }
        #endregion
    }
}
